// AdminController.java
package com.neusoft.SPNursingHome.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.neusoft.SPNursingHome.po.Admin;
import com.neusoft.SPNursingHome.po.Client;
import com.neusoft.SPNursingHome.po.ResponseBean;
import com.neusoft.SPNursingHome.po.UserSession;
import com.neusoft.SPNursingHome.service.AdminService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    // 登录
    @PostMapping("/login")
    public ResponseBean<String> login(@RequestParam String account,
                                      @RequestParam String password) {
        return adminService.login(account, password);
    }

    @GetMapping("/me")
    public ResponseBean<Admin> getCurrentUser(
            @AuthenticationPrincipal UserSession userSession) {
        return adminService.getById(userSession.getAdminId());
    }

    /** 重置用户密码为其手机号后 6 位 */
    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/{id}/reset-password")
    public ResponseBean<Void> resetPassword(@PathVariable Integer id) {
        return adminService.resetPasswordToPhoneSuffix(id);
    }

    // CRUD 接口
    @GetMapping("/list")
    public ResponseBean<IPage<Admin>> list(
            @RequestParam(required = false) String name,
            @RequestParam(defaultValue = "1") long page,
            @RequestParam(defaultValue = "10") long size,
            @RequestParam(required = false) String role    // 新增 role 参数
    ) {
        IPage<Admin> result = adminService.listAll(name, role, page, size);
        return new ResponseBean<>(result);
    }


    // 查询身份为健康管家的列表
    @GetMapping("/listCareAd")
    public ResponseBean<IPage<Admin>> listCareAd(
            @RequestParam(required = false) String name,
            @RequestParam(defaultValue = "1") long page,
            @RequestParam(defaultValue = "10") long size) {
        IPage<Admin> result = adminService.listCareAd(name, page, size);
        return new ResponseBean<>(result);
    }

    @GetMapping("/{id}")
    public ResponseBean<Admin> get(@PathVariable Integer id) {
        return adminService.getById(id);
    }

    //管理员移除某管家的客户
    @PostMapping("/unassign/{clientId}")
    public ResponseBean<Void> unassignClient(@PathVariable Integer clientId) {
        return adminService.unassignClient(clientId);
    }

    @PostMapping("/loginWithOutRedis")
    public ResponseBean<Admin> loginWithOutRedis(@RequestParam String account,
                                                 @RequestParam String password) {
        return adminService.loginWithOutRedis(account, password);
    }

    @PostMapping("/create")
    public ResponseBean<Void> create(@RequestBody Admin admin) {
        return adminService.create(admin);
    }

    @PutMapping("/update")
    public ResponseBean<Void> update(@RequestBody Admin admin) {
        return adminService.update(admin);
    }

    @DeleteMapping("/delete")
    public ResponseBean<Void> delete(Integer id) {
        return adminService.delete(id);
    }

    // 根据管理员ID查询其所护理的客户
    @GetMapping("/clients/{adminId}")
    public ResponseBean<IPage<Client>> listClients(@PathVariable Integer adminId,
                                                  @RequestParam(defaultValue="1") long page,
                                                  @RequestParam(defaultValue="10") long size) {
        return adminService.listClientsByAdmin(adminId,page,size);
    }
}
